package org.me.control_panel;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import android.widget.*;
import android.content.Intent;

import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.bluetooth.BluetoothAdapter;
import android.provider.Settings.System;
import java.lang.reflect.Method;
import android.net.wifi.WifiConfiguration;
import android.view.WindowManager;

public class Main extends Activity {

    TextView text;
    RadioGroup BluetoothCtrl,WirelessCtrl,
            BrightnessCtrl, OrientationCtrl, HotspotCtrl,
            Volume1Ctrl,Volume2Ctrl,Volume3Ctrl;

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.main);
        text = (TextView)findViewById(R.id.text);
        BluetoothCtrl = (RadioGroup)findViewById(R.id.Bluetooth);
        WirelessCtrl = (RadioGroup)findViewById(R.id.Wireless);
        BrightnessCtrl = (RadioGroup)findViewById(R.id.Brightness);
        OrientationCtrl = (RadioGroup)findViewById(R.id.AutoRotate);
        HotspotCtrl = (RadioGroup)findViewById(R.id.Hotspot);
        Volume1Ctrl = (RadioGroup)findViewById(R.id.Volume1);
        Volume2Ctrl = (RadioGroup)findViewById(R.id.Volume2);
        Volume3Ctrl = (RadioGroup)findViewById(R.id.Volume3);
    }

    @Override
    public void onResume() {
        super.onResume();
        CheckWireless();
        CheckBluetooth();
        CheckBrightness();
        CheckOrientation();
        CheckVolume1();
        CheckVolume2();
        CheckVolume3();
    }

    void CheckWireless() {
        try {
            WifiManager WF = (WifiManager)getSystemService(WIFI_SERVICE);
            switch (WF.getWifiState()) {
                case WifiManager.WIFI_STATE_ENABLED:
                    WirelessCtrl.check(R.id.Wireless1); break;
                case WifiManager.WIFI_STATE_DISABLED:
                    WirelessCtrl.check(R.id.Wireless0); break;
            }
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    public void WirelessCmd(View v) {
        try {
            WifiManager WF = (WifiManager)getSystemService(WIFI_SERVICE);
            switch (WirelessCtrl.getCheckedRadioButtonId()) {
                case R.id.Wireless0: WF.setWifiEnabled(false); break;
                case R.id.Wireless1: WF.setWifiEnabled(true); break;
            }
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

    void CheckBluetooth() {
        try {
            BluetoothAdapter BT = BluetoothAdapter.getDefaultAdapter();
            //BluetoothCtrl.setChecked(BT.getState() == );
            switch (BT.getState()) {
                case BluetoothAdapter.STATE_ON:
                    BluetoothCtrl.check(R.id.Bluetooth1); break;
                case BluetoothAdapter.STATE_OFF:
                    BluetoothCtrl.check(R.id.Bluetooth0); break;
            }
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    /*
    void CheckBluetooth() {
        try {
            int value = System.getInt(getContentResolver(),
                    System.BLUETOOTH_ON);
            BluetoothCtrl.setChecked(value == 1);
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    */
    public void BluetoothCmd(View v) {
        try {
            BluetoothAdapter BT = BluetoothAdapter.getDefaultAdapter();
            switch (BluetoothCtrl.getCheckedRadioButtonId()) {
                case R.id.Bluetooth0: BT.disable(); break;
                case R.id.Bluetooth1: BT.enable(); break;
                case R.id.Bluetooth2: {
                    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 60);
                    startActivity(intent);
                }
            }
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

    void CheckBrightness() {
        try {
            int value = System.getInt(getContentResolver(),
                    System.SCREEN_BRIGHTNESS);
            switch (value) {
                case 0: BrightnessCtrl.check(R.id.Brightness0); break;
                case 64: BrightnessCtrl.check(R.id.Brightness1); break;
                case 127: BrightnessCtrl.check(R.id.Brightness2); break;
                case 192: BrightnessCtrl.check(R.id.Brightness3); break;
                case 255: BrightnessCtrl.check(R.id.Brightness4); break;
            }
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    public void BrightnessCmd(View v) {
        try {
            int value = 255;
            switch (BrightnessCtrl.getCheckedRadioButtonId()) {
                case R.id.Brightness0: value = 0; break;
                case R.id.Brightness1: value = 64; break;
                case R.id.Brightness2: value = 127; break;
                case R.id.Brightness3: value = 192; break;
                case R.id.Brightness4: value = 255; break;
            }
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.screenBrightness = (float)(value==0?1:value)/255;
            getWindow().setAttributes(layoutParams);
            System.putInt(getContentResolver(),
            System.SCREEN_BRIGHTNESS,value);
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

    public void CheckOrientation() {
        try {
            switch (System.getInt(getContentResolver(),
                    System.ACCELEROMETER_ROTATION)) {
                case 0:
                    OrientationCtrl.check(R.id.Orientation0); break;
                case 1:
                    OrientationCtrl.check(R.id.Orientation1); break;
            }
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    public void OrientationCmd(View v) {
        try {
            int AutoRotate = System.getInt(getContentResolver(),
                    System.ACCELEROMETER_ROTATION);
            switch (OrientationCtrl.getCheckedRadioButtonId()) {
                case R.id.Orientation0: AutoRotate = 0; break;
                case R.id.Orientation1: AutoRotate = 1; break;
            }
            System.putInt(getContentResolver(),
                    System.ACCELEROMETER_ROTATION,AutoRotate);
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

    void CheckVolume1() {
        try {
            AudioManager AM = (AudioManager)getSystemService(AUDIO_SERVICE);
            int Max = AM.getStreamMaxVolume(AudioManager.STREAM_RING);
            float Vol = AM.getStreamVolume(AudioManager.STREAM_RING);
            switch (Math.round(Vol*4/Max)) {
                case 0: Volume1Ctrl.check(R.id.Volume1_0); break;
                case 1: Volume1Ctrl.check(R.id.Volume1_1); break;
                case 2: Volume1Ctrl.check(R.id.Volume1_2); break;
                case 3: Volume1Ctrl.check(R.id.Volume1_3); break;
                case 4: Volume1Ctrl.check(R.id.Volume1_4); break;
            }
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    public void Volume1Cmd(View v) {
        try {
            float Vol, Max;
            AudioManager AM = (AudioManager)getSystemService(AUDIO_SERVICE);
            Vol = Max = AM.getStreamMaxVolume(AudioManager.STREAM_RING);
            switch (Volume1Ctrl.getCheckedRadioButtonId()) {
                case R.id.Volume1_0: Vol = 0; break;
                case R.id.Volume1_1: Vol = 1*Max/4; break;
                case R.id.Volume1_2: Vol = Max/2; break;
                case R.id.Volume1_3: Vol = 3*Max/4; break;
                case R.id.Volume1_4: Vol = Max; break;
            }
            AM.setStreamVolume(AudioManager.STREAM_RING,Math.round(Vol),0);
            switch (AM.getRingerMode()) {
                case AudioManager.RINGER_MODE_SILENT:
                case AudioManager.RINGER_MODE_VIBRATE:
                    if (Vol > 0) AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL); break;
                case AudioManager.RINGER_MODE_NORMAL: 
                    if (Vol == 0) AM.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); break;
            }
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

    void CheckVolume2() {
        try {
            AudioManager AM = (AudioManager)getSystemService(AUDIO_SERVICE);
            int Max = AM.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            float Vol = AM.getStreamVolume(AudioManager.STREAM_MUSIC);
            switch (Math.round(Vol*4/Max)) {
                case 0: Volume2Ctrl.check(R.id.Volume2_0); break;
                case 1: Volume2Ctrl.check(R.id.Volume2_1); break;
                case 2: Volume2Ctrl.check(R.id.Volume2_2); break;
                case 3: Volume2Ctrl.check(R.id.Volume2_3); break;
                case 4: Volume2Ctrl.check(R.id.Volume2_4); break;
            }
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    public void Volume2Cmd(View v) {
        try {
            float Vol, Max;
            AudioManager AM = (AudioManager)getSystemService(AUDIO_SERVICE);
            Vol = Max = AM.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            switch (Volume2Ctrl.getCheckedRadioButtonId()) {
                case R.id.Volume2_0: Vol = 0; break;
                case R.id.Volume2_1: Vol = 1*Max/4; break;
                case R.id.Volume2_2: Vol = Max/2; break;
                case R.id.Volume2_3: Vol = 3*Max/4; break;
                case R.id.Volume2_4: Vol = Max; break;
            }
            AM.setStreamVolume(AudioManager.STREAM_MUSIC,Math.round(Vol),0);
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

        void CheckVolume3() {
        try {
            AudioManager AM = (AudioManager)getSystemService(AUDIO_SERVICE);
            int Max = AM.getStreamMaxVolume(AudioManager.STREAM_ALARM);
            float Vol = AM.getStreamVolume(AudioManager.STREAM_ALARM);
            switch (Math.round(Vol*4/Max)) {
                case 0: Volume3Ctrl.check(R.id.Volume3_0); break;
                case 1: Volume3Ctrl.check(R.id.Volume3_1); break;
                case 2: Volume3Ctrl.check(R.id.Volume3_2); break;
                case 3: Volume3Ctrl.check(R.id.Volume3_3); break;
                case 4: Volume3Ctrl.check(R.id.Volume3_4); break;
            }
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }
    public void Volume3Cmd(View v) {
        try {
            float Vol, Max;
            AudioManager AM = (AudioManager)getSystemService(AUDIO_SERVICE);
            Vol = Max = AM.getStreamMaxVolume(AudioManager.STREAM_ALARM);
            switch (Volume3Ctrl.getCheckedRadioButtonId()) {
                case R.id.Volume3_0: Vol = 0; break;
                case R.id.Volume3_1: Vol = 1*Max/4; break;
                case R.id.Volume3_2: Vol = Max/2; break;
                case R.id.Volume3_3: Vol = 3*Max/4; break;
                case R.id.Volume3_4: Vol = Max; break;
            }
            AM.setStreamVolume(AudioManager.STREAM_ALARM,Math.round(Vol),0);
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

    public void HotspotCmd(View v) {
        try {
            boolean enabled = false;
            switch (HotspotCtrl.getCheckedRadioButtonId()) {
                case R.id.Hotspot0: enabled = false; break;
                case R.id.Hotspot1: enabled = true; break;
            }
            WifiManager wifi = (WifiManager)getSystemService(WIFI_SERVICE);
            if (enabled) wifi.setWifiEnabled(false);
            Method method = wifi.getClass().getDeclaredMethod("setWifiApEnabled",
                new Class[]{WifiConfiguration.class,boolean.class});
            method.invoke(wifi,null,enabled);
            exit();
        }
        catch (Exception ex) {
            text.setText(ex.toString());
        }
    }

    protected void exit() {
        this.finish();
        //int pid = android.os.Process.myPid();
        //android.os.Process.killProcess(pid);
    }

}
